﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Shop.Logic
{
    class Controller
    {
        DAO dao;
        View view1;
        String LoginUser;
        public Controller()
        {
            Shop.Program.DataIO.ToView = new Shop.Program.DataIO.MyEvent(events);
            dao = new DAO();
            view1 = new View();
        }
        void events(List<Object> param)
        {
            Query(param);
        }

        public bool Autorization(string name,string password)
        {
            if (dao.isExists("Select surname from Seller where Surname='" + name + "' and password ='" + password + "'"))
            {
                LoginUser = name;
                view1.login.Text = name;
                view1.Text = "Терминал || Пользователь " + name;
                view1.Show();
                return true;
            }
            return false;
        }

        void Query(List<Object> param)
        {
            if ((param.Count == 3)||(param.Count == 2))
            {
                switch ((string)param[0])
                {
                    case "Select":
                        DataGridView temp = (DataGridView)param[2];
                        try
                        {
                            temp.DataSource = (DataTable)dao.ObjectReturn((string)param[1]);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }
                        break;
                    case "Insert":
                        Label labelVeiew = (Label)param[2];
                        if (dao.ControlData((String)param[1]))
                            labelVeiew.Text = "Данные добавлены";
                        else
                        labelVeiew.Text = "Ошибка при добавлении";
                        break;
                    case "Delete":
                        labelVeiew = (Label)param[2];
                        if (dao.ControlData((String)param[1]))
                            labelVeiew.Text = "Удалено";
                        else
                            labelVeiew.Text = "Ошибка при удалении";
                        break;
                    case "Update":
                        labelVeiew = (Label)param[2];
                        if (dao.ControlData((String)param[1]))
                            labelVeiew.Text = "Данные добавлены";
                        else
                            labelVeiew.Text = "Ошибка при добавлении";
                        break;
                    case "Select List":
                        ComboBox tempControl = (ComboBox)param[2];
                        List<String> tempsList = new List<string>();
                        try
                        {
                            tempsList = dao.ExecuteParams((string)param[1]);
                            for (int i = 0; i < tempsList.Count; i++)
                            {
                                tempControl.Items.Add(tempsList[i]);
                            }
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }
                        break;
                } 
            }
            else if ((param.Count == 1) && (param[0] is string))
            {
                MessageBox.Show(param[0].ToString());
            }
        }
    }

}
